package com.itheima.mall.controller;


import com.itheima.mall.pojo.ProductAttribute;
import com.itheima.mall.pojo.ProductAttributeCategory;
import com.itheima.mall.service.PmsProductAttributeCategoryService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    //添加商品属性分类
    @PostMapping("/create")
    public ResponseEntity createProductAttributeCategory(@RequestParam String name){
        pmsProductAttributeCategoryService.createProductAttribute(name);
        return ResponseEntity.ok("");
    }

    //分页显示商品属性分类
    @GetMapping("/list")
    public ResponseEntity productAttributeCategoryList(@RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "10") Integer pageSize){
       PageResult pageResult = pmsProductAttributeCategoryService.productAttributeList(pageNum , pageSize);

       return ResponseEntity.ok(pageResult);
    }
    //通过id删除商品属性
    @GetMapping("/delete/{id}")
    public ResponseEntity deleteProductAttributeById(@PathVariable("id") Integer id){
       pmsProductAttributeCategoryService.deleteProductAttributeById(id);
       return ResponseEntity.ok("");
    }

    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/list/withAttr")
    public ResponseEntity listWithAttr(){
       List<ProductAttributeCategory> list = pmsProductAttributeCategoryService.listWithAttr();
        return ResponseEntity.ok(list);
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateProductAttributeCategoryById(@PathVariable("id") Integer id , @RequestParam("name") String name){
        pmsProductAttributeCategoryService.updateProductAttributeCategoryById(id , name);
        return ResponseEntity.ok("");
    }

    /**
     * 获取单个商品属性分类信息
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    public ResponseEntity selectById(@PathVariable("id") Integer id){
        ProductAttributeCategory pac = pmsProductAttributeCategoryService.selectById(id);
        return ResponseEntity.ok(pac);
    }
}
